package com.gitee.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.client.RestTemplate;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.SpanNameProvider;

import zipkin.Span;
import zipkin.reporter.AsyncReporter;
import zipkin.reporter.Reporter;
import zipkin.reporter.Sender;
import zipkin.reporter.okhttp3.OkHttpSender;

@Configuration
@Order(value = 1)
public class ZipkinConfig {
    @Value("${spring.application.name}")
    private String application;

    @Value("${zipkin.address}")
    private String address;

    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    Sender sender() {
        return OkHttpSender.create(address);

    }

    @Bean
    Reporter<Span> reporter() {
        return AsyncReporter.builder(sender()).build();
    }

    @Bean
    Brave brave() {
        return new Brave.Builder(application).reporter(reporter()).build();
    }

    @Bean
    SpanNameProvider spanNameProvider() {
        return new DefaultSpanNameProvider();
    }

}
